# Maintainer: David Runge <dvzrv@archlinux.org>

# NOTE: dependents need rebuild on minor release

pkgbase=libiio
pkgname=(
  libiio
  python-pylibiio
)
pkgver=0.25
pkgrel=2
pkgdesc="Interface to the Linux Industrial Input/Output (IIO) Subsystem"
arch=(loong64 x86_64)
url="https://github.com/analogdevicesinc/libiio"
license=(
  GPL-2.0-or-later
  LGPL-2.1-or-later
)
makedepends=(
  avahi
  cmake
  libaio
  libserialport
  libusb
  libxml2
  python-setuptools
)
source=(
  $url/archive/v$pkgver/$pkgname-v$pkgver.tar.gz
  0001-Install-man-pages-to-the-correct-chapter-directories.patch
)
sha512sums=('e0f2f5545b4c78d1f0f56b037db6e363aab01aad69af3a095b3546af74dab6effe0b00f5c336ef799a739f76c5f16ab76a6b8e823508861edd2745a9d3fd2599'
            '4703e37584fba0824c6c93bf5289edd258d9ebe3b6d84fa608cad455346130be982176a2da2523d9bddfb3e3468c8ba992a088ae855868a612446a9d36251b3b')
b2sums=('9b3190b5c69fc9fc624f4b0c686cace828fc34219a986a306ffba6b4c2a4f3b8c60cd8d5c67c350033d83ecdaeb010559318bbf76f0b0565bdbcbe119d85e097'
        '2149bad4cd0b4048cb7d37d0592896b55be4e7ddd80f7322fa42a45b0369226f831d6f7869a5c0d7fdea356d43f7e9fe1538c94962fd6360399c121a5c4a22cf')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  # fix installation directory for man pages: https://github.com/analogdevicesinc/libiio/pull/1057
  patch -Np1 -d $pkgname-$pkgver -i ../0001-Install-man-pages-to-the-correct-chapter-directories.patch
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_LIBDIR=lib
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_INSTALL_SBINDIR=bin
    -D PYTHON_BINDINGS=ON
    -D SYSTEMD_UNIT_INSTALL_DIR=/usr/lib/systemd/system
    -D UDEV_RULES_INSTALL_DIR=/usr/lib/udev/rules.d
    -D WITH_MAN=ON
    -D WITH_SYSTEMD=ON
    -D WITH_SERIAL_BACKEND=ON
    -S $pkgname-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure
}

package_libiio() {
  depends+=(
    avahi libavahi-client.so libavahi-common.so
    glibc
    libaio
    libserialport
    libxml2
    libusb libusb-1.0.so
  )
  optdepends=(
    'python-pylibiio: for Python bindings'
  )
  provides=(
    libiio.so
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname-$pkgver/{CONTRIBUTING,Contributors,README}.md -t "$pkgdir/usr/share/doc/$pkgname"

  (
    cd "$pkgdir"
    _pick python-pylibiio usr/lib/python*
  )
}

package_python-pylibiio() {
  pkgdesc+=" - Python bindings"
  depends=(
    libiio=$pkgver
    python
  )

  mv -v $pkgname/* "$pkgdir"
}
